//----------------------------------Include-------------------------------------
#include <string.h>
#include <stdlib.h>
#include "mcu.h"
#include "glbvar.h"
//------------------------------------------------------------------------------
//global var defined in this file
#define  DBG_GLOBALS
#include "dbg.h"
//------------------------------------------------------------------------------

//---------------------------------Global Var-----------------------------------
//------------------------------------------------------------------------------

//-----------------------------------Macro--------------------------------------
//for rx
#define RXBUF_LEN                 (64)
//------------------------------------------------------------------------------

//------------------------------------Type--------------------------------------
typedef struct{
  unsigned int   cnt;
  unsigned int   done;
  char           data[RXBUF_LEN];
}t_rcv;
//------------------------------------------------------------------------------

//---------------------------------Static Var-----------------------------------
//for rx
static t_rcv            nt_rcv;
//------------------------------------------------------------------------------

//--------------------------------Static Func-----------------------------------
static char* _check_rx(uint32_t* sz)
{
  *sz = 0;
  if(!nt_rcv.done) return NULL;
  if(!nt_rcv.cnt ) return NULL;
  *sz = nt_rcv.cnt;
  if(nt_rcv.cnt >= RXBUF_LEN) nt_rcv.data[RXBUF_LEN-1] = 0;
  else                        nt_rcv.data[nt_rcv.cnt]  = 0;
  return &nt_rcv.data[0];
}

static void _clear_rx(void)
{
  nt_rcv.done = 0;
  nt_rcv.cnt  = 0;
}
//------------------------------------------------------------------------------
static char* _strstr(char *haystack, char *needle, int hn, int nn)
{
  int   i, j, first;
  char* p;
  first = 1;
  for(i=0, j=0; i<hn && j<nn; i++){
    if(haystack[i] != needle[j]){ p=NULL; first=1; j=0; continue; }
    if(first){p = haystack+i; first=0;}
    j++;
  }
  return p;
}
//------------------------------------------------------------------------------
//protocol
//[0~2]start  [3]len  [4]val  [5]0x0D  [6]0x0A
//0xAA55AA
static uint32_t _rx_info(char* p, uint32_t sz)
{
  uint8_t   len;
  uint8_t   val;
  
  len = p[3];
  if(sz < len) return sz;
  if(len < 7)  return sz;
  if(p[5] != 0x0D || p[6] != 0x0A) return sz;
  
  val = p[4];
  if(val>0 && val<=100) gu32_turns = val;
  
  return len;
}

static void _parse(char* pbuf, uint32_t sz)
{
  static char   frm_st[]  = {0xAA, 0x55, 0xAA};
  static char   frm_end[] = {0x0D, 0x0A};
  char          *phead, *ptail, *pcurt;
  int           left;
  
  pcurt = pbuf;
  left  = sz;
  do{
    phead = _strstr(pcurt, frm_st,  left, sizeof(frm_st));
    if(phead == NULL)          break;
    if(phead >= pcurt + left)  break;
    
    ptail = _strstr(pcurt, frm_end, left, sizeof(frm_end));
    if(ptail == NULL)          break;
    if(ptail >= pcurt + left)  break;
    
    if(ptail <= phead) break;
    _rx_info(phead, (phead - ptail) + sizeof(frm_end));
    
#if 1   //if not continuously, set to 1
    break;
#else
    pcurt = ptail + sizeof(frm_end);
    left  = left - (pcurt - phead);
    if(left <= 0) break;
#endif
  }while(1);
}
//------------------------------------------------------------------------------

//--------------------------------Public Func-----------------------------------
static  char*     p;
static  uint32_t  sz;
void dbg_cyc(void)
{
  p = _check_rx(&sz);  if(!p) return;
  _parse(p, sz);
  _clear_rx();
}
//------------------------------------------------------------------------------
#if 1
//for interrupt receive
void it_dbg_rcv_one_byte(unsigned char c)
{
  if(nt_rcv.cnt >= RXBUF_LEN){
    nt_rcv.done = 0;
    nt_rcv.cnt  = 0;
    return;
  }
  nt_rcv.data[nt_rcv.cnt++] = c;

  if(nt_rcv.cnt >= 2){
    if(nt_rcv.data[nt_rcv.cnt-2] == 0x0D){
      if(nt_rcv.data[nt_rcv.cnt-1] == 0x0A){
        nt_rcv.done = 1;
      }else{
        nt_rcv.done = 0;
        nt_rcv.cnt  = 0;
      }
    }
  }
}

void it_dbg_rcv_done(void)
{
  nt_rcv.done = 1;
}
#endif
//------------------------------------EOF---------------------------------------




